﻿using Com.Lancetinc.Evs.Resources;
using System;
using System.IO;
using System.Windows.Media;

namespace Com.Lancetinc.Evs.ViewModels
{
    public class ImageViewModel : ViewModelBase
    {
        /// <summary>
        /// 选择？
        /// </summary>
        private bool _selected = false;
        public bool Selected
        {
            get => _selected;
            set => SetProperty(ref _selected, value);
        }

        /// <summary>
        /// 图片名
        /// </summary>
        public string Name
        {
            get;
        }

        public string ImageFullPath
        {
            get
            {
                if (ImageFullPathProvider == null)
                    return null;
                return ImageFullPathProvider(Name);
            }
        }

        private ImageSource _imageSource;
        public ImageSource ImageSource
        {
            get
            {
                if (_imageSource != null)
                    return _imageSource;

                var imgFile = ImageFullPath;
                if (ImageFullPath == null || !File.Exists(imgFile))
                    return null;

                _imageSource = ResourceHelper.GetImageFromFile(imgFile);
                return _imageSource;
            }
        }

        public void ToggleSelected()
        {
            Selected = !Selected;
        }

        public ImageViewModel(string name)
        {
            this.Name = name;
        }

        public Func<string, string> ImageFullPathProvider { get; set; }
    }
}